// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()

  try {
    const result = await cloud.openapi.subscribeMessage.send({
      touser: cloud.getWXContext().OPENID, // 通过 getWXContext 获取 OPENID
      page: `/pages/blog-comment/blog-comment?blogId=${event.blogId}`,
      lang: 'zh_CN',
      data: {
        phrase1: {
          value: '评价成功'
        },
        thing2: {
          value: event.content
        }
      },
      templateId: event.templateId,
      miniprogramState: 'developer'
    })
    // result 结构
    // { errCode: 0, errMsg: 'openapi.templateMessage.send:ok' }
    return result
  } catch (err) {
    // 错误处理
    // err.errCode !== 0
    return err
  }
}